import os.path
import time

from selenium import webdriver

# root_dir=os.path.dirname(os.path.abspath(__file__))

class TestCookielogin():
    def setup_class(self):
        self.driver=webdriver.Chrome()

    # def teardown(self):
    #     self.driver.quit()

    def test_get_cookies(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        # 等待20s，人工扫码操作
        time.sleep(20)
        #等成功登陆之后，再去获取cookie信息
        cookie=self.driver.get_cookies()
        print(cookie)

    def test_add_cookie(self):
        #访问企业微信主页面
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        #读取cookie信息
        cookies=[{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'direct'}, {'domain': '.work.weixin.qq.com', 'expiry': 1756373195, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '0'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '23475882091598642'}, {'domain': '.work.weixin.qq.com', 'expiry': 1727429195, 'httpOnly': False, 'name': 'ww_lang', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'cn,zh'}, {'domain': '.work.weixin.qq.com', 'expiry': 1727429207, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'zh'}]
        #把cookie信息添加到浏览器
        for cookie in cookies:
            self.driver.add_cookie(cookie)
        #刷新页面
        self.driver.refresh()

    # def write_cookies_to_file(self,cookies):
    #     with open(f"{root_dir}/data/cookies.yaml","w",encoding='utf-8') as f:
    #         yaml.safe_dump(cookies,f)
    #
    # def read_cookies_from_file(self):
    #     with open(f"{root_dir}/data/cookies.yaml", encoding='utf-8') as f:
    #         cookies=yaml.safe_load(f)
    #         return cookies